Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reduce React re-renders when checking currentCard() #3398

Merged
merged 7 commits into from
Jul 11, 2024

Conversation

DafyddLlyr
Copy link
Contributor

@DafyddLlyr DafyddLlyr commented Jul 10, 2024

What's the problem?

What's the cause?

When running the application in dev mode, the following two errors generally coincide with the above occurrence -

image image

The stack traces of these errors point to the following lines of code respectively -

https://github.com/theopensystemslab/planx-new/blob/main/editor.planx.uk/src/@planx/components/Section/Public.tsx#L31

After a lot of trial and error debugging these sections it essentially appears that the problem is caused by upcomingCardIds() (called when hitting "back") clashing with the rendering of the AnalyticsProvider (which passed the updated node down to it's descendants).

What's the solution?

  • Refactor currentCard()
    • This is a function with side effects - it updates state in the Zustand store by calling upcomingCards() and is ultimately responsible for displaying a card to the user
    • This should only be called once, when the continue button or back button is hit (via record())
    • Generally currentCard() is called as a shorthand to get the current node Id, so I've made a new property in the store to hold this variable
    • This has the (very helpful) side effect of reducing the number of re-renders as we're no longer frequently calling upcomingCardIds(), just referring to the stored currentCard() from the last call.
  • Remove node from the AnalyticsProvider, and just store in Zustand
    • This resolves the errors we've been seeing, as well as making sense from a hierarchy POV

- `currentCard()` is called multiple times which triggers the expensive `upcomingCardIds()`
- Adding a static `currentCard` variable reduced calls (and thus multiple React re-renders)
Copy link

github-actions bot commented Jul 10, 2024

Removed vultr server and associated DNS entries

@DafyddLlyr DafyddLlyr force-pushed the dp/current-card-multiple-renders-bug branch from 83c2eaa to e27818c Compare July 10, 2024 11:22
@DafyddLlyr DafyddLlyr force-pushed the dp/current-card-multiple-renders-bug branch from e27818c to c4f9bd2 Compare July 10, 2024 15:46
@@ -48,7 +48,7 @@ const Card: React.FC<Props> = ({
...props
}) => {
const theme = useTheme();
const [path, currentCard] = useStore((state) => [
const [path, visibleNode] = useStore((state) => [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout this PR I've tired to maintain existing variable names when deconstructing state from the store. I think this minimises the number of changes, and hopefully maintains context within the component. For example, sometimes the currentCard is referred to as the node, visibleNode or currentCard across differing components.

@@ -379,6 +380,7 @@ export const previewStore: StateCreator<

resumeSession(session: Session) {
set({ ...session });
get().setCurrentCard();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When resuming, we handle the initial state setup here after getting a ReconciliationResponse

Please see #1701 for context.

@DafyddLlyr DafyddLlyr requested a review from a team July 10, 2024 15:59
@DafyddLlyr DafyddLlyr marked this pull request as ready for review July 10, 2024 15:59
Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All working as expected for me & pizza feels snappy 👏

Really valuable refactor here even if unfortunately not root of August's issue yet !!

@DafyddLlyr DafyddLlyr merged commit 495bb41 into main Jul 11, 2024
16 checks passed
@DafyddLlyr DafyddLlyr deleted the dp/current-card-multiple-renders-bug branch July 11, 2024 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants